<template>
  <div id="app">
    <component is="mainpage" ref="mainPage"></component>
  </div>
</template>

<script>
import mainpage from "./main.vue";
export default {
  name: "App",
  components:{
    mainpage
  },
  methods: {
    getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) return r[2];
      return "";
    },
    getRequestAuthUrl(redirectUrl, candidateId) {
      this.$http
        .get(
          this.$DEV_ENV_API +
            "/api/getRequestAuthUrlBase?redirectUrl=" +
            this.$DEV_ENV_API +
            "/api/public/yixing/index.html&state=" +
            candidateId
        )
        .then(res => {
          if (res.data.code === "1") {
            console.log(res.data);
            window.location.href = res.data.data;
          } else {
            console.log(JSON.stringify(res));
          }
        })
        .catch(err => {});
    },
    getUserInfo(code) {
      this.$http
        .get(this.$DEV_ENV_API + "/api/getWXUserInfo?code=" + code)
        .then(res => {
          sessionStorage.setItem("openid", res.data.data.openid);
        })
        .catch(err => {});
    }
  },
  mounted() {
    let isWeiXin = () => {
      return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
    };
    if (isWeiXin()) {
      let type = this.getQueryString("type");
      if (type == 1) {
        //WX
        let candidateId = this.getQueryString("candidateId");
        let redirectUrl = this.$DEV_ENV_API + "/api/public/yixing/index.html";
        this.getRequestAuthUrl(redirectUrl, candidateId);
      } else {
        //Other
        let code = this.getQueryString("code");
        let candidateId = this.getQueryString("state");
        if (code !== "") {
          this.getUserInfo(code);
          if (candidateId != "") {
            if (isNaN(parseInt(candidateId))) {
                this.$root.$emit("navigatorTo",candidateId);
            } else {
              this.$router.push({
                name: "Detail",
                params: {
                  candidateId: candidateId
                }
              });
            }
          }
        }
      }
    } else {
      document.head.innerHTML =
        '<title>抱歉，出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="//res.wx.qq.com/open/libs/weui/0.4.3/weui.min.css">';
      document.body.innerHTML =
        '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">请在微信客户端打开链接</h4></div></div>';
    }
  }
};
</script>

<style>
html,
body {
  height: 100%;
  width: 100%;
  margin: 0px;
  overflow-x: hidden;
  background-color: rgb(247, 211, 232);
}
a {
  text-decoration: none;
}
#app {
  height: 100%;
  font-family: Helvetica;
}
.flex {
  display: flex;
}
.flex-v {
  display: flex !important;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.full-width {
  width: 100%;
}
.full-height {
  height: 100%;
}
span {
  font-size: 12px;
}
p {
  font-size: 10px;
}
</style>
